﻿using Architecture.API.Application.Command.CityCommand;
using Architecture.Domain;
using Architecture.Domain.Service.CityServices;
using Architecture.Repository.Interface;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--市
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class CityManagementController : ControllerBase
    {
        /// <summary>
        /// 市仓储接口
        /// </summary>
        protected readonly ICityService _cityService;

        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="cityService">市仓储接口</param>
        /// <param name="mediator">命令总线</param>
        public CityManagementController(ICityService cityService, IMediator mediator)
        {
            _cityService = cityService;
            _mediator = mediator;
        }

        /// <summary>
        /// 获取指定省市的所有市
        /// </summary>
        /// <param name="provinceId">省ID</param>
        /// <returns>返回市列表</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult<List<City>>> GetCities(int provinceId)
        {
            ApiResult<List<City>> result = new ApiResult<List<City>>();
            try
            {
                result.Data= await _cityService.GetAllAsync(provinceId);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取指定省市的所有市--MediatR模式
        /// </summary>
        /// <param name="command">查询命令</param>
        /// <returns>返回市列表</returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<List<City>>> Search(CitySearchCommand command)
        {
            ApiResult<List<City>> result = new ApiResult<List<City>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }

}

